﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;

namespace FileServer
{
    class FileTransfer
    {
        private Socket clientSocket;
        private string downloadTo;
        private int bufferSize;

        public FileTransfer(Socket clientsock, string downloadinfo)
        {
            clientSocket = clientsock;
            downloadTo = downloadinfo;
            bufferSize = 1024;
        }

        public bool downloadData()
        {
            try
            {
                System.Diagnostics.Debug.WriteLine("Started to Recive Data");
                int receivedBytesLen = 0;
                FileStream neofiledata = new FileStream(downloadTo, FileMode.Append, FileAccess.Write);
                
                byte[] buffer = new byte[bufferSize];
                
                do
                {
                    receivedBytesLen = clientSocket.Receive(buffer);
                    neofiledata.Write(buffer, 0, bufferSize);
                } while (receivedBytesLen != 0);

                neofiledata.Close();
                //clientSocket.Close();
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err);
            }
            return true;
        }
    }
}
